import json

from fastapi import Depends

from app.core.BaseResponse import success, fail
from app.model import mysql
from app.service.comment import create_media_comment, media_root_comments, get_comment_children, \
    get_comment_object_first_comment
from app.service.jwt_service import get_current_user
from app.service.media import get_base_media, get_media
from app.service.response import response_comment, response_second_comment, response_detail_media, \
    response_detail_media_photo
from app.settings.config import LIMIT
from app.validator.app import schemas


async def photo_detail(
        id: int,
        current_user: schemas.User = Depends(get_current_user),
):
    """"""
    media_id = id
    media = await get_media(media_id)
    if not media or media.is_delete or not media.data:
        return fail(-1, '作品已被删除')
    if media.object_type != mysql.Media.Type.photo:
        return fail(-1, '作品不存在')

    data = await response_detail_media(current_user.id, media.id, media=media)
    return success(data)


async def multiple_detail(
        id: int,
        current_user: schemas.User = Depends(get_current_user),
):
    """"""
    media_id = id
    media = await get_media(media_id)
    if not media or media.is_delete or not media.data:
        return fail(-1, '作品已被删除')
    if media.object_type != mysql.Media.Type.case:
        return fail(-1, '作品不存在')

    items = []
    detail = await response_detail_media(current_user.id, media.id, media=media)
    items.append(detail)
    return success(items)


async def video_detail(
        id: int,
        current_user: schemas.User = Depends(get_current_user),
):
    """"""
    media_id = id
    media = await get_media(media_id)
    if not media or media.is_delete or not media.data:
        return fail(-1, '作品已被删除')
    if media.object_type != mysql.Media.Type.video:
        return fail(-1, '作品不存在')

    data = await response_detail_media(current_user.id, media.id, media=media)
    return success(data)


async def graphic_detail(
        id: int,
        current_user: schemas.User = Depends(get_current_user),
):
    """"""
    media_id = id
    media = await get_media(media_id)
    if not media or media.is_delete or not media.data:
        return fail(-1, '作品已被删除')
    if media.object_type != mysql.Media.Type.case:
        return fail(-1, '作品不存在')

    detail = await response_detail_media(current_user.id, media.id, media=media)

    # 评论
    root_comments = await media_root_comments(media_id, offset=0, limit=LIMIT)
    items = []
    for c in root_comments:
        item = await response_comment(current_user.id, comment_id=c.id, comment=c)
        if not item:
            continue
        items.append(item)
    data = {
        "detail": detail,
        "commentList": items,
        "commentNumber": media.comment_number,
    }
    return success(data)


async def detail_comments(
        id: int,
        page: int = 1,
        current_user: schemas.User = Depends(get_current_user),
):
    """"""
    media_id = id
    limit = LIMIT
    offset = (page - 1) * limit
    media = await get_base_media(media_id)
    if not media:
        return fail(-1, '作品不存在，无法获取评论列表')
    root_comments = await media_root_comments(media_id, offset=offset, limit=limit)
    items = []
    for c in root_comments:
        item = await response_comment(current_user.id, comment_id=c.id, comment=c)
        if not item:
            continue
        items.append(item)
    data = {
        'commentNumber': media.comment_number,
        'commentList': items
    }
    return success(data)


async def detail_comment_comments(
        id: int,
        commentId: int,
        page: int = 1,
        current_user: schemas.User = Depends(get_current_user),
):
    """"""
    comment_id = commentId
    limit = LIMIT
    offset = (page - 1) * limit
    exclude_ids = []
    comment_f = await get_comment_object_first_comment(comment_id)
    if comment_f:
        exclude_ids.append(comment_f.id)
    # 获取二级列表
    comments = await get_comment_children(comment_id, offset=offset, limit=limit, exclude_ids=exclude_ids)
    items = []
    for c in comments:
        item = await response_second_comment(c.id, c)
        if not item:
            continue
        items.append(item)
    return success(items)


async def create_detail_comment(
        id: int,
        create_comment: schemas.CreateComment,
        current_user: schemas.User = Depends(get_current_user)
):
    """"""
    media_id = id
    reply_revert_comment_id = create_comment.commentId or 0
    text = create_comment.comment or ''
    comment = await create_media_comment(current_user.id, media_id, reply_revert_comment_id, text)
    if not comment:
        return fail(-1, '评论创建失败')
    # 返回两种格式
    if reply_revert_comment_id:  # 返回二级评论
        data = await response_second_comment(comment.id, comment=comment)
    else:  # 返回一级评论
        data = await response_comment(current_user.id, comment_id=comment.id, comment=comment)
    if not data:
        return fail(-1, '评论创建失败')
    return success(data)


async def detail_recommend(
        id: int,
        page: int = 1,
        current_user: schemas.User = Depends(get_current_user)
):
    """"""
    recommend_ids = [1]
    items = []
    for recommend_id in recommend_ids:
        item = await response_detail_media_photo(current_user.id, recommend_id)
        if item:
            items.append(item)
    return success(items)
